// pages/device/index.js
import naviConfigs from './navi.js';
import {common, debounce} from './common.js';
let ddlAssets;
var app =getApp();
Page({
  onReady: function () {
    ddlAssets = this.selectComponent("#ddlAssets")
  },
  /**
   * 组件的属性列表
   */
  properties: {
    id:"",
  },    
  /**
   * 组件的初始数据
   */
  data: {
    naviConfigs: naviConfigs,
    naviconfig:null,
    inputShowed: false,
    inputVal: '',
    id:123,
    scrollTopArr:[],
    show: true,
    width:120,
    marginleft:65,
    marginleft0:95,
    searchresult:'请输入要查询的编号',
    findit:false,
    searchtext:'          搜索',
    newdevice:{icon: '/icons/yamianji.jpg',
    title: '',
    order:'',
    navigateMark: '',
    nearopentime:'',
    type:''},
    array: ['图片1', '图片2', '图片3'],
    objectArray: [
      {
        id: 1,
        name: '图片1'
      },{
        id: 2,
        name: '图片2'
      },{
        id: 3,
        name: '图片3'
      },
    ],
    index: 0,
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  upload() {
    // 1.选择图片  ,官方文档api
    // https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseImage.html
    wx.chooseImage({
      count: 1,//当前图片选择个数, 小程序最多支持一下选择9张
      sizeType: ['original', 'compressed'], //['源文件','压缩文件']
      sourceType: ['album', 'camera'],//文件来源  ['相册','摄像头牌照']
      success:res=>{
        // tempFilePath可以作为img标签的src属性显示图片(图片临时路径)
        const tempFilePaths = res.tempFilePaths
        var index0=this.data.index
        console.log(tempFilePaths) //tempFilePaths是数组
        // 官方api   https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/storage/api.html
        wx.cloud.uploadFile({
          //cloudPath: 'example.png', // 上传至云端的路径
　　　　　　　　//图片名字 先用时间戳代替了,看自己喜好
　　　　　　　　cloudPath: index0 +'.png',
          filePath: tempFilePaths[0], // 小程序临时文件路径
          success: res => {
            // 返回文件 ID
            console.log(res.fileID)
            db.collection('image').add({
              data: {
                fileID: res.fileID
              }
            }).then(res=>{
              console.log(res)
            }).catch(err=>{
              console.error(err)
            })
          },
          fail: console.error
        })
      }
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {

  },

  test:function (params) {
    console.log('on click');
  },
  
  addnewdevice(){
    var newtype='';
   wx.showActionSheet({
     itemList: ['甲型','乙型','丙型'],
     success:(res)=>{
       switch(res.tapIndex){
         case 0:
           newtype='甲型'
           break;
         case 1:
           newtype='乙型'
           break;
         case 2:
           newtype='丙型'
           break;
       }
       this.data.id++
      naviConfigs.shopConfig.addCard({
          icon: '/icons/yamianji.jpg',
          title: '压面机编号：'+this.data.id,
          order:this.data.id,
          navigateMark: '关机',
          nearopentime:'',
          type:newtype
      })
      },
     fail(res){

     }
   })
  },

  onPageScroll: function(e) {
    const that = this
    this.setData({show: false});
    debounce(function(){
        that.setData({show: true});
    },500)
  },

  onLoad: function (options) {

    wx.setNavigationBarTitle({
      title: '首页'
    })
  },
  
  loading(){
    wx.showToast({
      title:'请稍候',
      duration:2000,
      icon:'loading',
      mask:true
    })
  },

  //点击设备卡片，
  specificfr: function () {

    // console.log(app.globalData.goEasy)
    this.loading()
    //带设备id参数跳转到具体设备详情页面
    wx.navigateTo({
      url: '/pages/specificfr/index?id='+this.data.id,
    })
  },
  //搜索跳转
  deviceinfo0: function () {
    this.loading()
    //带设备id参数跳转到具体设备详情页面
    if(this.data.findit==true){
    wx.navigateTo({
      url: '/pages/specificdevice/index?id='+this.data.id,
    })
    }
  },

  view(){
wx.showModal({
  title: '设备概况',
  content: '设备总数：3'+'\r\n'+'甲型总数：1'+'\r\n'+'乙型总数：1'+'\r\n'+'丙型总数：1'+'\r\n'+'运行总数：1'+'\r\n'+'故障总数：0',
  complete: (res) => {
    if (res.cancel) {
      
    }

    if (res.confirm) {
      
    }
  }
})
  },

  showInput() {
    this.setData({
      inputShowed: true,
    });
  },

  hideInput() {
    this.setData({
      inputVal: '',
      inputShowed: false,
      width:120,
      marginleft:65,
      marginleft0:95,
      searchtext:'          搜索',
    });
  },
dddd(){
this.setData({
  inputShowed: true,
  marginleft:-5,
  width:210,
  marginleft0:-7,
  searchtext:'  搜索...',
});
},
  clearInput() {
    this.setData({
      inputVal: '',
    });
  },

  inputTyping(e) {
    this.setData({
      inputVal: e.detail.value,
    });
    let naviconfigs=naviConfigs.shopConfig;
    for(var index in naviconfigs){
       if(naviconfigs[index].order==e.detail.value){
        this.setData({
          findit:true,
          naviconfig : naviconfigs[index],
          id:naviconfigs[index].order,
          searchresult:naviconfigs[index].title+"        "+naviconfigs[index].type+"           "+naviconfigs[index].navigateMark,
        });
        break;
       }
       this.setData({
        searchresult: '请输入要查询的编号',
        naviconfig : null,
        findit:false,
      });
    }
  },

})